/*
Content: Generate extra treatment dates for do-file 9_... 
Paper: "Immigrants Return Intentions and Labor Market Behavior when the Home Country is Unsafe"
Journal: Journal of Labor Economics
Authors: Jacopo Bassetto, Teresa Freitas-Monteiro
*/


********************************************************************************
**# LOAD DATASET OF PRE-POST SETUP
********************************************************************************

use "$data\prepost_setup_smonthly_ieb.dta", clear

drop if ieb_nat_id==0 // Germany
drop if treatment_terror_m3y_p90==. // months in between that at the moment we dont use

********************************************************************************
**# FIRST ATTACK DATE
********************************************************************************

* Redefine startmy
 
	// Note: The control group is 3 months before the date of the treated, so I want to assign them the same start day as the treated but 3 months before

rename startmy startmy_orig

clonevar startmy=startmy_orig if treatment_terror_m3y_p90==0

replace startmy=startmy+3 // I assign them the treatment date to merge again to allevents_monthly_ieb and then just subtract 3 months again
replace startmy=startmy_orig if treatment_terror_m3y_p90==1 // now both control and treatment have the date of the treated

* Merge again all terrors
merge m:1 ieb_nat_id startmy using "$globalterror/final/allevents_monthly_ieb.dta", keepusing(startdate) keep(1 3) 
sort ieb_nat_id startmy_orig
format startdate %td

rename startmy event_startmy
rename startmy_orig startmy

* For the control I now want to assign the control month which is 3 months before the treated month and is correct in startmy since is the original date
gen day=day(startdate)
gen month=month(dofm(startmy)) if startdate!=.
gen year=year(dofm(startmy)) if startdate!=.

g startdate2=mdy(month,day,year)
format startdate2 %td

br if startdate2==. // assigns 31 to months that only have 30 days

replace day=30 if day==31 & startdate2==.
replace day=29 if inrange(day,30,31) & month==2 & startdate2==. // february

drop startdate2
g startdate2=mdy(month,day,year)
format startdate2 %td

br if startdate2==. // assigns 31 to months that only have 30 days
replace day=28 if inrange(day,29,31) & month==2 & startdate2==. // february

drop startdate2
g startdate2=mdy(month,day,year)
format startdate2 %td

br if startdate2==. 


drop _merge day month year 
rename startdate2 firstattack_date


********************************************************************************
**# FIXED NUMBER OF DAYS BEFORE THE ATTACK
********************************************************************************

foreach x in 15 {
g firstattack_`x'd_date=firstattack_date-`x'
format firstattack_`x'd_date %td
}

foreach x in   15 {
	clonevar treatment_terror_firstattack_`x'd=treatment_terror_m3y_p90
}

save "$globalterror/final/prepost_extra_startdates.dta", replace


********************************************************************************
**# SAME STARTMY BETWEEN CONTROL AND TREATED
********************************************************************************
	
*** When the 15/10 before fall into a different month startmy will be incorrect

preserve
keep startmy ieb_nat_id event_startmy firstattack_date firstattack_20d_date treatment_terror_firstattack_20d
gen month=month(dofm(startmy)) 
gen month2=month(firstattack_20d_date)
gen year=year(firstattack_20d_date)
g diffmonth=1 if month!=month2 & month!=. & month2!=.
keep if diffmonth==1
g startmy_c=ym(year,month2)
format startmy_c %tm

drop month month2 startmy  diffmonth year
rename startmy_c startmy
order startmy
g new20d=1
tempfile datat20d
save `datat20d'

restore

append using `datat20d'
sort ieb_nat_id startmy startdate


foreach x in 15 10 5 {
preserve
keep startmy ieb_nat_id event_startmy firstattack_date firstattack_`x'd_date treatment_terror_firstattack_`x'd

gen month=month(dofm(startmy)) 
gen month2=month(firstattack_`x'd_date)
gen year=year(firstattack_`x'd_date)
g diffmonth=1 if month!=month2 & month!=. & month2!=.
keep if diffmonth==1
g startmy_c=ym(year,month2)
format startmy_c %tm

drop month month2 startmy  diffmonth year
rename startmy_c startmy
order startmy
g new`x'd=1
tempfile datat`x'd
save `datat`x'd'

restore

cap drop _merge
merge 1:1 startmy ieb_nat_id event_startmy firstattack_date  using `datat`x'd'
sort ieb_nat_id startmy startdate
}

** just so that is a totally new data set
clonevar treatment_firstattack=treatment_terror_m3y_p90
drop treatperiod_terror_m3y_p90 treatment_terror_m3y_p90
drop _merge
cap drop _merge
save "$globalterror/final/\prepost_extra_startdates.dta", replace